package com.icss.clothes.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.support.converter.DefaultClassMapper;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RabbitMQConfig {
    //1、创建用于订单的交换机
    @Bean
    public DirectExchange createExchange(){
        return new DirectExchange("ticket_direct_exchange");
    }
    //创建订单消息队列
    @Bean
    public Queue ticketQueue(){
        return new Queue("ticket-queue");
    }
    //实现交换机和消息队列的绑定
    @Bean
    public Binding createBinding(){
        return new Binding("ticket-queue",
                Binding.DestinationType.QUEUE,
                "ticket_direct_exchange",
                "ticket-key",
                null);
    }
    @Bean
    public MessageConverter jsonToMapMessageConverter() {
        DefaultClassMapper defaultClassMapper = new DefaultClassMapper();
        defaultClassMapper.setTrustedPackages("com.icss.clothes.rabbitmq"); // trusted packages
        Jackson2JsonMessageConverter jackson2JsonMessageConverter = new Jackson2JsonMessageConverter();
        jackson2JsonMessageConverter.setClassMapper(defaultClassMapper);
        return jackson2JsonMessageConverter;
    }
}
